package com.example.anquan.Service;

import com.example.anquan.guanli.Article;
import com.example.anquan.guanli.Comment;
import com.example.anquan.repository.ArticleRepository;
import com.example.anquan.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class CommentService {
    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    private ArticleRepository articleRepository;

    public List<Comment> findCommentByArticleId(Integer id){
        return commentRepository.findByArticleId(id);
    }

    public void addComment(Integer id, String author, String content){
        Comment comment = new Comment();
        Article article = articleRepository.findById(id).get();
        comment.setArticle(article);
        comment.setAuthor(author);
        comment.setContent(content);
        comment.setDate(new Date());
        commentRepository.save(comment);
    }
    public void deleteDiscuss(Integer id) {
        commentRepository.deleteDiscuss(id);
    }

    ;
    public List<Comment> findAll() {
        return commentRepository.findAll();
    }

    ;
}
